import Vue from 'vue'
let BASE_URL, ENV, sassAppId = '0',channel = 'activity-weixin-h5';
// #ifdef H5
channel = 'activity-weixin-h5'
// #endif
// #ifdef MP-WEIXIN
channel = 'yahe-wechat-mini'
// #endif
// #ifdef MP-TOUTIAO
channel = 'event-douyin-mini'
// #endif
// #ifdef H5
const Vconsole = require('@/utils/vconsole.min.js')
if (process.env.NODE_ENV == 'production') {
	if (location) {
		if (location.hostname == 'sit.tuan.ant.shop') {
			const con = new Vconsole()
			BASE_URL = 'sit.api.yahedso.com'
			ENV = 'sitactivity'
		} else {
			BASE_URL = 'https://api.tuan.ues.xyz' //   正式
			ENV = 'prodevent'
		}
	} else {
		const con = new Vconsole()
		BASE_URL = 'https://sit.mp.shopdt.cn' //开发
	}

} else {
	const con = new Vconsole()
	BASE_URL = 'https://sit.mp.shopdt.cn' //开发
}
// #endif
/*#ifdef MP*/
  BASE_URL = 'https://sit.api.yahedso.com'
/*#endif*/
export {
	BASE_URL,
	ENV,
	sassAppId,
	channel
}

export const get = async (url, params, load, config = {}) => {
	return await new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			data: params,
			timeout: 300000,
			method: 'GET',
			header: {
				channel,
				sassAppId,
				token: uni.getStorageSync('token'),
				...config,
			},
			success: res => {
				console.log('请求成功', BASE_URL + url, res.data,params)
				const {
					code
				} = res.data
				if (code == 401) {
					uni.showToast({
						title: '您的登录信息已失效，请重新登录。',
						icon: 'none'
					})
					setTimeout(() => {
						uni.handleLogin()
					}, 2000)
					return;
				}
				resolve(res.data)
				/** end 处理结束*/
			},
			fail: error => {
				reject(error)
			},
			complete: res => {
				if (load) uni.hideLoading();
			}
		})
	})
}

export const post = async (url, params, load, config = {}) => {
	return await new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			data: params,
			timeout: 300000,
			method: 'POST',
			header: {
				channel,
				sassAppId,
				token: uni.getStorageSync('token'),
				...config,
			},
			success: res => {
				console.log('请求成功', BASE_URL + url, res.data,params)
				const {
					code
				} = res.data
				if (code == 401) {
					uni.showToast({
						title: '您的登录信息已失效，请重新登录。',
						icon: 'none'
					})
					setTimeout(() => {
						uni.handleLogin()
					}, 2000)
					return;
				}
				resolve(res.data)
				/** end 处理结束*/
			},
			fail: error => {
				reject(error)
			},
			complete: res => {
				if (load) uni.hideLoading();
			}
		})
	})
}

export const put = async (url, params, load, config = {}) => {
	return await new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			data: params,
			timeout: 300000,
			method: 'PUT',
			header: {
				channel,
				sassAppId,
				token: uni.getStorageSync('token'),
				...config,
			},
			success: res => {
				const {
					code
				} = res.data
				if (code == 401) {
					uni.showToast({
						title: '您的登录信息已失效，请重新登录。',
						icon: 'none'
					})
					setTimeout(() => {
						uni.handleLogin()
					}, 2000)
					return;
				}
				resolve(res.data)
				/** end 处理结束*/
			},
			fail: error => {
				reject(error)
			},
			complete: res => {
				if (load) uni.hideLoading();
			}
		})
	})
}